package com.debao.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.debao.common.constant.Constants;
import com.debao.system.domain.*;
import com.debao.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.debao.system.service.ISignInService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 课程签到Service业务层处理
 *
 * @author DeBao
 * @date 2024-07-19
 */
@Service
public class SignInServiceImpl implements ISignInService {
    @Autowired
    private SignInMapper signInMapper;

    @Resource
    private CourseCardMapper courseCardMapper;
    @Resource
    private TimeUserCardMapper timeUserCardMapper;

    @Resource
    private CourseTypeMapper courseTypeMapper;
    @Resource
    private UserCourseCardMapper userCourseCardMapper;

    /**
     * 查询课程签到
     *
     * @param id 课程签到主键
     * @return 课程签到
     */
    @Override
    public SignIn selectSignInById(Long id) {
        return signInMapper.selectSignInById(id);
    }

    /**
     * 查询此课程是否已经被用户签到
     *
     * @param userId
     * @param
     * @return
     */
    @Override
    public Boolean selectSignInByUserId(Long userId, Long cardId, String date) {
        return signInMapper.selectSignInByUserId(userId, cardId, date);
    }

    /**
     * 查询课程签到列表
     *
     * @param signIn 课程签到
     * @return 课程签到
     */
    @Override
    public List<SignIn> selectSignInList(SignIn signIn) {
        return signInMapper.selectSignInList(signIn);
    }

    /**
     * 新增课程签到
     *
     * @param signIn 课程签到
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSignIn(SignIn signIn) {
        //签到成功后，判断是否是周期卡还是次卡，周期卡不用管，次卡需要剩余次数减1，使用次数加1
        CourseCard courseCard = courseCardMapper.selectCourseCardById(signIn.getCourseCardId());
        if (Objects.isNull(courseCard)) {
            throw new RuntimeException("课程卡不存在");
        }

        CourseType courseType = courseTypeMapper.selectCourseTypeById(courseCard.getTypeId());
        if (Objects.isNull(courseType)) {
            throw new RuntimeException("课程卡类型不存在");
        }
        //天卡的状态 不能过期
        if (courseType.getType() == Constants.DAY_CARD) {
            UserCourseCard userCourseCard = userCourseCardMapper.selectUserCourseCardByIdAndCourseId(signIn.getUserId(), signIn.getCourseCardId());
            if (Objects.isNull(userCourseCard) && userCourseCard.getStatus() == Constants.STATUS_N) {
                throw new RuntimeException("周期卡不存在或已过期");
            }

        }
        // 次卡，更新剩余次数和使用次数
        if (courseType.getType() == Constants.TIME_CARD) {
            TimeUserCard timeUserCard = timeUserCardMapper.selectTimeUserCardByUserId(signIn.getUserId());
            if (Objects.isNull(timeUserCard) && timeUserCard.getSurplus() <= 0) {
                throw new RuntimeException("用户可卡不存在,或课程卡次数不足");
            }
            int updNum = timeUserCardMapper.updNum(timeUserCard.getId());
            if (updNum != 1) {
                throw new RuntimeException("更新用户卡可用次数失败");
            }
        }
        //todo 短信通知和微信长期订阅通知
        return signInMapper.insertSignIn(signIn);
    }

    /**
     * 修改课程签到
     *
     * @param signIn 课程签到
     * @return 结果
     */
    @Override
    public int updateSignIn(SignIn signIn) {
        return signInMapper.updateSignIn(signIn);
    }

    /**
     * 批量删除课程签到
     *
     * @param ids 需要删除的课程签到主键
     * @return 结果
     */
    @Override
    public int deleteSignInByIds(Long[] ids) {
        return signInMapper.deleteSignInByIds(ids);
    }

    /**
     * 删除课程签到信息
     *
     * @param id 课程签到主键
     * @return 结果
     */
    @Override
    public int deleteSignInById(Long id) {
        return signInMapper.deleteSignInById(id);
    }
}
